﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace InvasionWars
{
    public partial class GameBoard : Form
    {
        private ClientServerConn gameServerComm;
        private int zoneCount;
        private Zone[] zones;
        private int dragSourceIndex;
        public GameBoard()
        {
            InitializeComponent();
            CardReader.ReadArmyCards(Armies.HUMAN);
            zoneCount = 5;
            
            
        }

        private void GameBoard_Load(object sender, EventArgs e)
        {

            ServerConnection serverConnection = new ServerConnection();
            serverConnection.ShowDialog();
            if (serverConnection.DialogResult == DialogResult.Cancel)
            {
                this.Close();

            }
            else if (serverConnection.DialogResult == DialogResult.OK)
            {
                gameServerComm = serverConnection.JoinedGame;

                Trace.WriteLine(gameServerComm.ServerName, "Server Name");
                Trace.WriteLine(gameServerComm.Port, "Game Port");


                //this is a test game;
                tsLblStatus.Text = "Game Started";
                gameServerComm.Connect();
                
                Thread chatListenThread = new Thread(new ThreadStart(chatListener));
                chatListenThread.Start();
                initializeBoard();
                
            }
        }


        public void initialize()
        {
            //send an initialize request to the server
            gameServerComm.Initialize();
            //PARSE DATA

            //after initialize I need to check the player.Army and assign the card backs based on that.

            do
            {

            } while (!gameServerComm.thisPlayer.initializeComplete);

            
        }

        public void initializeBoard()
        {

            initialize();
            System.Drawing.Image playerCardBack;
            System.Drawing.Image oppCardBack;
            if(gameServerComm.thisPlayer.Army == Armies.HUMAN)
            {
                playerCardBack = System.Drawing.Image.FromFile("Resources\\EarthForceBack.png");
                oppCardBack = System.Drawing.Image.FromFile("Resources\\AlienForceBack.png");
            }
            else
            {
                playerCardBack = System.Drawing.Image.FromFile("Resources\\AlienForceBack.png");
                oppCardBack = System.Drawing.Image.FromFile("Resources\\EarthForceBack.png");
            }
            //init the zones
            zones = new Zone[5];
            for (int i = 0; i < zoneCount; i++)
            {
                zones[i] = new Zone(i);
            }
            

            //set the hand
            pBxHand1.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[0].ImageName);
            pBxHand2.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[1].ImageName);
            pBxHand3.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[2].ImageName);
            pBxHand4.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[3].ImageName);
            pBxHand5.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[4].ImageName);
            pBxHand6.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[5].ImageName);
            

            //set the deck
            pBxDeck.Image = playerCardBack;
            pBxDeckOpp.Image = oppCardBack;

            //set the enemy cards
            pBxOppHand1.Image = oppCardBack;
            pBxOppHand2.Image = oppCardBack;
            pBxOppHand3.Image = oppCardBack;
            pBxOppHand4.Image = oppCardBack;
            pBxOppHand5.Image = oppCardBack;
            pBxOppHand6.Image = oppCardBack;
        }


        public void chatListener()
        {
            while (gameServerComm != null && gameServerComm.serverConnectionState == ServerConnectionState.CONNECTED)
            {
                try
                {
                    string message = gameServerComm.CheckForChat();
                    if (message != null)
                        displayMessage(message, "Opponent");
                }
                catch (NullReferenceException nre)
                {
                    //do nothing hopefully this will terminate
                }
            }
        }

        public void sendChat(string message)
        {
            displayMessage(message, "You");
            gameServerComm.SendChat(message);
        }

        private delegate void displayDelegate(string message, string sender);

        private void displayMessage(string message, string sender)
        {
            if (txtChatBox.InvokeRequired)
            {
                Invoke(new displayDelegate(displayMessage), new object[] { message, sender});
            }
            else
            {
                txtChatBox.Text += "\r\n"  + sender +": " + message;

            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            sendChat(txtCompose.Text);
            txtCompose.Text = "";
        }

        private void GameBoard_FormClosing(object sender, FormClosingEventArgs e)
        {

            //if (gameServerComm.serverConnectionState == ServerConnectionState.CONNECTED)
            //    gameServerComm.Disconnect();

            //null out everything
            dragDestination = null;
            dragSource = null;
            zones = null;
            gameServerComm = null;
            

            //this.Close();
        }


        /**
         *      drag drop Methods and properties
         * 
         */

        private PictureBox dragSource;
        private PictureBox dragDestination;


        private void PlayerHandMouseDown(object sender, MouseEventArgs e)
        {
            this.AcceptButton = btnComplete;
            if (((PictureBox)sender).Image != null)
            {
                dragSource = (PictureBox)sender;
                dragSourceIndex = GetHandID((PictureBox)sender);
                DoDragDrop(((PictureBox)sender).Image, DragDropEffects.All);
                
            }
        }

        private int GetHandID(PictureBox pictureBox)
        {
            if (pictureBox.Name == "pBxHand1")
                return 0;
            else if (pictureBox.Name == "pBxHand2")
                return 1;
            else if (pictureBox.Name == "pBxHand3")
                return 2;
            else if (pictureBox.Name == "pBxHand4")
                return 3;
            else if (pictureBox.Name == "pBxHand5")
                return 4;
            else if (pictureBox.Name == "pBxHand6")
                return 5;
            else
                return -1;
        }
        



        private void DragEnterGameField(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap) && ((PictureBox)sender).Image == null)
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;

        }

        private void DragDrop(object sender, DragEventArgs e)
        {

            if ((e.Data.GetDataPresent(DataFormats.Bitmap)) )
            {
                ((PictureBox)sender).Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
                int dragDestinationIndex = GetZoneID((PictureBox)sender);
                gameServerComm.Update(dragSourceIndex, dragDestinationIndex);
                if (gameServerComm.thisPlayer.Army == Armies.HUMAN)
                    zones[dragDestinationIndex].HumanSlot = gameServerComm.thisPlayer.Hand[dragSourceIndex];
                else
                    zones[dragDestinationIndex].AlienSlot = gameServerComm.thisPlayer.Hand[dragSourceIndex];

                dragSource.Image = null;
                dragSource.Refresh();
                dragSource = null;
                dragSourceIndex = -1;
                //send an update to the server:
                
            }

        }

        private int GetZoneID(PictureBox pictureBox)
        {
            if (pictureBox.Name == "pBxZone1")
                return 0;
            else if (pictureBox.Name == "pBxZone2")
                return 1;
            else if (pictureBox.Name == "pBxZone3")
                return 2;
            else if (pictureBox.Name == "pBxZone4")
                return 3;
            else if (pictureBox.Name == "pBxZone5")
                return 4;
            else
                return -1;
        }

        private void btnComplet_Click(object sender, EventArgs e)
        {
            //write data to the server for the server to process.
            
            //se the values from the hand and zones

            gameServerComm.UpdateComplete();

            do
            {

            } while (gameServerComm.ChangesRead);

            zones = gameServerComm.Zones;
            //set the hand
            pBxHand1.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[0].ImageName);
            pBxHand2.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[1].ImageName);
            pBxHand3.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[2].ImageName);
            pBxHand4.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[3].ImageName);
            pBxHand5.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[4].ImageName);
            pBxHand6.Image = System.Drawing.Image.FromFile("Resources\\" + gameServerComm.thisPlayer.Hand[5].ImageName);

            if (gameServerComm.thisPlayer.Army == Armies.HUMAN)
            {
                if (zones[0].HumanSlot != null)
                    pBxZone1.Image = System.Drawing.Image.FromFile("Resources\\" + zones[0].HumanSlot.ImageName);
                else
                    pBxZone1.Image = null;

                if (zones[1].HumanSlot != null)
                    pBxZone2.Image = System.Drawing.Image.FromFile("Resources\\" + zones[1].HumanSlot.ImageName);
                else
                    pBxZone2.Image=null;

                if (zones[2].HumanSlot != null)
                    pBxZone3.Image = System.Drawing.Image.FromFile("Resources\\" + zones[2].HumanSlot.ImageName);
                else
                    pBxZone3.Image = null;

                if (zones[3].HumanSlot != null)
                    pBxZone4.Image = System.Drawing.Image.FromFile("Resources\\" + zones[3].HumanSlot.ImageName);
                else
                    pBxZone4.Image = null;

                if (zones[4].HumanSlot != null)
                    pBxZone5.Image = System.Drawing.Image.FromFile("Resources\\" + zones[4].HumanSlot.ImageName);
                else
                    pBxZone5.Image = null;
                
                
                if (zones[0].AlienSlot != null)
                    pBxZone1Opp.Image = System.Drawing.Image.FromFile("Resources\\" + zones[0].AlienSlot.ImageName);
                else
                    pBxZone1Opp.Image = null;

                if (zones[1].AlienSlot != null)
                    pBxZone2Opp.Image = System.Drawing.Image.FromFile("Resources\\" + zones[1].AlienSlot.ImageName);
                else
                    pBxZone2Opp.Image = null;

                if (zones[2].AlienSlot != null)
                    pBxZone3Opp.Image = System.Drawing.Image.FromFile("Resources\\" + zones[2].AlienSlot.ImageName);
                else
                    pBxZone3Opp.Image = null;

                if (zones[3].AlienSlot != null)
                    pBxZone4Opp.Image = System.Drawing.Image.FromFile("Resources\\" + zones[3].AlienSlot.ImageName);
                else
                    pBxZone4Opp.Image = null;

                if (zones[4].AlienSlot != null)
                    pBxZone5Opp.Image = System.Drawing.Image.FromFile("Resources\\" + zones[4].AlienSlot.ImageName);
                else
                    pBxZone5Opp.Image = null;
            }
            else
            {
                if(zones[0].HumanSlot != null)
                    pBxZone1Opp.Image = System.Drawing.Image.FromFile("Resources\\" + zones[0].HumanSlot.ImageName);
                else
                    pBxZone1Opp.Image = null;

                if (zones[1].HumanSlot != null)
                    pBxZone2Opp.Image = System.Drawing.Image.FromFile("Resources\\" + zones[1].HumanSlot.ImageName);
                else
                    pBxZone2Opp.Image=null;

                if (zones[2].HumanSlot != null)
                    pBxZone3Opp.Image = System.Drawing.Image.FromFile("Resources\\" + zones[2].HumanSlot.ImageName);
                else
                    pBxZone3Opp.Image = null;

                if (zones[3].HumanSlot != null)
                    pBxZone4Opp.Image = System.Drawing.Image.FromFile("Resources\\" + zones[3].HumanSlot.ImageName);
                else
                    pBxZone4Opp.Image = null;

                if (zones[4].HumanSlot != null)
                    pBxZone5Opp.Image = System.Drawing.Image.FromFile("Resources\\" + zones[4].HumanSlot.ImageName);
                else
                    pBxZone5Opp.Image = null;
                
                if (zones[0].AlienSlot != null)
                    pBxZone1.Image = System.Drawing.Image.FromFile("Resources\\" + zones[0].AlienSlot.ImageName);
                else
                    pBxZone1.Image = null;

                if (zones[1].AlienSlot != null)
                    pBxZone2.Image = System.Drawing.Image.FromFile("Resources\\" + zones[1].AlienSlot.ImageName);
                else
                    pBxZone2.Image = null;

                if (zones[2].AlienSlot != null)
                    pBxZone3.Image = System.Drawing.Image.FromFile("Resources\\" + zones[2].AlienSlot.ImageName);
                else
                    pBxZone3.Image = null;

                if (zones[3].AlienSlot != null)
                    pBxZone4.Image = System.Drawing.Image.FromFile("Resources\\" + zones[3].AlienSlot.ImageName);
                else
                    pBxZone4.Image = null;

                if (zones[4].AlienSlot != null)
                    pBxZone5.Image = System.Drawing.Image.FromFile("Resources\\" + zones[4].AlienSlot.ImageName);
                else
                    pBxZone5.Image = null;
            }
            

        }

        private void txtCompose_Enter(object sender, EventArgs e)
        {
            this.AcceptButton = btnSend;
        }

        private void txtCompose_Leave(object sender, EventArgs e)
        {
            this.AcceptButton = btnComplete;
        }
    }
}
